package com.teng.smartdrawer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.teng.smartdrawer.R;
import com.teng.smartdrawer.bean.Component;
import com.teng.smartdrawer.bean.Module;
import com.teng.smartdrawer.bean.Part;
import com.teng.smartdrawer.bean.Recommend;
import com.teng.smartdrawer.parser.DataParser;
import com.teng.smartdrawer.utils.ImageLoaderTools;
import com.teng.smartdrawer.utils.ImageLoadingConfig;
import com.teng.smartdrawer.utils.Utils;
import com.teng.smartdrawer.view.CircleImageView;

import java.util.List;

/**
 * Created by teng on 16/2/20.
 */
public class RecommendAdapter extends BaseAdapter {

    private Context context;
    private List<Recommend> mData;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions imgOptions = ImageLoadingConfig.generateDisplayImageOptions(R.mipmap.icon);

    public RecommendAdapter(Context context, List<Recommend> mData) {
        this.context = context;
        this.mData = mData;
    }

    private View.OnClickListener onCountChangeListener;

    public void setOnCountChangeListener(View.OnClickListener onCountChangeListener) {
        this.onCountChangeListener = onCountChangeListener;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (mData != null) {
            ret = mData.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
//        long ret = 0;
//        if (mData != null && mData.get(position) != null) {
//            ret = mData.get(position).getId();
//        }
        return position;
    }

    @Override
    public int getViewTypeCount() {

        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        int ret = 0;
        Recommend recommend = mData.get(position);
        if (recommend instanceof  Component){
            ret = 0;
        }else if(recommend instanceof Module){
            ret = 1;
        }
        return ret;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
       Recommend recommend = mData.get(position);

        if (recommend instanceof Component){
            ret = bindPartView(position, convertView, parent);
        }else {
            ret = bindModuleView(position, convertView, parent);
        }

        return ret;
    }

    View bindModuleView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        Module module = (Module) mData.get(position);
        if (convertView != null) {
            ret = convertView;
        }else{
            ret = LayoutInflater.from(context).inflate(R.layout.recommend_item , parent , false);
        }

        PartViewHolder partViewHolder = (PartViewHolder) ret.getTag();
        if (partViewHolder == null) {
            partViewHolder = new PartViewHolder();
            partViewHolder.partName = (TextView) ret.findViewById(R.id.name_recommend_item);
            partViewHolder.imageView = (ImageView) ret.findViewById(R.id.image);
            partViewHolder.parent_layout = (LinearLayout) ret.findViewById(R.id.parent_layout);

            ret.setTag(partViewHolder);
        }

        partViewHolder.partName.setText(module.getD_name()+"");
        String pic_url = module.getD_img();

        if (pic_url == null) {
            pic_url = "http://img2.pconline.com.cn/pconline/0706/19/1038447_34.jpg";
        }

        //pic_url = "http://img2.pconline.com.cn/pconline/0706/19/1038447_34.jpg";

        imageLoader.displayImage(pic_url, partViewHolder.imageView , imgOptions);
        return ret;
    }


    /**
     *  加载  推荐的零件
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    View bindPartView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        if (ret != null) {
            ret = convertView;
        }else {
            ret = LayoutInflater.from(context).inflate(R.layout.activity_part_adapter , parent , false);
        }

        ViewHolder viewHolder = (ViewHolder) ret.getTag();

        if (viewHolder == null) {
            viewHolder = new ViewHolder();
            viewHolder.iv_item_icon = (ImageView) ret.findViewById(R.id.iv_item_icon);
            viewHolder.reduce = (ImageView) ret.findViewById(R.id.reduce);
            viewHolder.reduce.setOnClickListener(onCountChangeListener);
            viewHolder.add = (ImageView) ret.findViewById(R.id.add);
            viewHolder.add.setOnClickListener(onCountChangeListener);

            viewHolder.tv_item_content = (TextView) ret.findViewById(R.id.tv_item_content);
            viewHolder.tv_item_name = (TextView) ret.findViewById(R.id.tv_item_name);

            viewHolder.put = (TextView) ret.findViewById(R.id.dt_put);
            viewHolder.put.setOnClickListener(onCountChangeListener);

            viewHolder.get = (TextView) ret.findViewById(R.id.dt_get);
            viewHolder.get.setOnClickListener(onCountChangeListener);

            viewHolder.dt_number = (TextView) ret.findViewById(R.id.dt_number);

            ret.setTag(viewHolder);
        }

        Component component = (Component) mData.get(position);

        String m_imgpc = component.getM_imgpc();

        imageLoader.displayImage(m_imgpc , viewHolder.iv_item_icon , imgOptions);

        viewHolder.tv_item_content.setText(component.getM_number()+"");

        viewHolder.tv_item_name.setText(component.getM_name() + "");

        int count = component.getCount();

        viewHolder.dt_number.setText(count + "");

        if (count == 0){
            viewHolder.reduce.setEnabled(false);
        }else {
            viewHolder.reduce.setEnabled(true);
        }

        viewHolder.put.setVisibility(View.VISIBLE);
        viewHolder.get.setVisibility(View.VISIBLE);

        viewHolder.add.setTag(position);
        viewHolder.reduce.setTag(position);
        viewHolder.put.setTag(position);
        viewHolder.get.setTag(position);

        return ret;
    }

    private static class PartViewHolder{
        public TextView partName;
        private ImageView imageView;
        private LinearLayout parent_layout;
    }



    private static class ViewHolder{

        public ImageView iv_item_icon , reduce , add;

        public TextView tv_item_name , tv_item_content , dt_number;

        public TextView put , get;

        private CheckBox checkBox;
    }
}
